import { _decorator, Component, director, instantiate, Node, Prefab, v3 } from 'cc';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('SceneMove')
export class SceneMove extends Component {


    tempPos=v3();
    tempPos2=v3();
    get moveSpeed(){
        return 220+(GameMgr.ins.PlayerScore*3);
    }

    start() {
        if (this.node.position.x > 600) {//开始游戏，如果这是第二个环境，就生成
            this.generateSpike();
        }
    }

    update(dt: number) {
        if (GameMgr.ins.IsGameIng) {
            // if (this.node.position.x<=-750) {
            //     this.node.setPosition(750,0)
            //     this.generateSpike();
            // }
            if (this.node.position.x <= -750) {
                this.node.getPosition(this.tempPos2)
                this.tempPos2.x += 750 * 2
                this.node.setPosition(this.tempPos2)
                this.generateSpike();
            }

            this.node.getPosition(this.tempPos);
            this.tempPos.x-=dt*this.moveSpeed;
            this.node.setPosition(this.tempPos)
        }
       
    }

    generateSpike(){
        this.node.getChildByName("land").removeAllChildren();

        let generateNum=1;
        for (let i = 0; i < generateNum; i++) {
            let pre=instantiate(GameMgr.ins.spikes[Math.round(Math.random()*2)]);
            pre.setParent(this.node.getChildByName("land"))
            let randPosx=GameMgr.ins.randowNum(-345,252)
            pre.setPosition(randPosx,140);
        }
        
    }

}


